<template>
  <div class="bg-white overflow-hidden">
    <div class="form-title">企业人员信息</div>

    <div class="form-container">
      <el-form
        ref="staffForm"
        v-loading="formLoading"
        :model="staffForm"
        label-width="6.25rem"
        :rules="formRules"
      >
        <div class="form-sub-title">主要人员情况</div>
        <table class="self-table-form" style="min-height: auto">
          <tr>
            <td class="m-label text-center w-36">类别</td>
            <td class="m-label text-center w-28">姓名</td>
            <td class="m-label text-center w-28">性别</td>
            <td class="m-label text-center w-36">学历</td>
            <td class="m-label text-center w-44">身份证号码</td>
            <td class="m-label text-center w-36">职务</td>
            <td class="m-label text-center w-28">职称</td>
            <td class="m-label text-center w-36">联系电话</td>
            <td class="m-label text-center w-40">邮箱</td>

            <td class="m-label text-center w-20">操作</td>
          </tr>

          <tr
            v-for="(item, index) in staffForm.situationList"
            :key="item.situationId || item.staffId"
          >
            <td>
              <el-form-item
                label-width="8rem"
                class="fixed-input-w"
                prop="type"
              >
                <el-select
                  v-if="index === 0 || index === 1"
                  id="type"
                  v-model="item.type"
                  filterable
                  :prop="'situationList.' + index + '.type'"
                  placeholder="请选择"
                  :disabled="Boolean(index === 0 || index === 1)"
                >
                  <el-option
                    v-for="item in mainTypeOp"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>

                <el-select
                  v-else
                  id="type"
                  v-model="item.type"
                  filterable
                  :prop="'situationList.' + index + '.type'"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in mainTypeOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>

                <!-- <el-input type="text" v-model="type"></el-input> -->
              </el-form-item>
            </td>

            <td class="m-label text-center">
              <el-form-item
                id="mainName"
                label=""
                :prop="'situationList.' + index + '.name'"
                :rules="formRules.mainName"
              >
                <el-input type="text" v-model="item.name" placeholder="姓名" />
              </el-form-item>
            </td>
            <td>
              <el-form-item
                id="sex"
                class="fixed-input-w"
                :prop="'situationList.' + index + '.sex'"
                :rules="{
                  required: true,
                  message: '请选择性别',
                  trigger: 'change',
                }"
              >
                <el-select v-model="item.sex" filterable placeholder="请选择">
                  <el-option
                    v-for="item in sexTypes"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </td>

            <td class="m-label text-center">
              <el-form-item
                label=""
                :prop="'situationList.' + index + '.degree'"
                :rules="{
                  required: true,
                  message: '请输入学历',
                  trigger: 'change',
                }"
              >
                <el-select
                  id="degree"
                  v-model="item.degree"
                  filterable
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in educationOptions"
                    :key="item.dictValue"
                    :label="item.dictLabel"
                    :value="item.dictValue"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </td>
            <td class="m-label text-center">
              <el-form-item
                id="mainIdentityCardNum"
                label=""
                :prop="'situationList.' + index + '.identityCardNum'"
                :rules="formRules.mainIdentityCardNum"
              >
                <el-input
                  type="text"
                  v-model="item.identityCardNum"
                  placeholder="身份证号码"
                />
              </el-form-item>
            </td>

            <td class="m-label text-center">
              <el-form-item
                id="mainJob"
                label=""
                :prop="'situationList.' + index + '.job'"
                :rules="formRules.mainJob"
              >
                <el-input type="text" v-model="item.job" placeholder="职务" />
              </el-form-item>
            </td>

            <td class="m-label text-center">
              <el-form-item
                id="professional"
                label=""
                :prop="'situationList.' + index + '.professional'"
                :rules="{
                  required: true,
                  message: '请输入职称',
                  trigger: 'change',
                }"
              >
                <el-select
                  id="jobNameOptions"
                  v-model="item.professional"
                  filterable
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in jobNameOptions"
                    :key="item.dictValue"
                    :label="item.dictLabel"
                    :value="item.dictValue"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </td>

            <td class="m-label text-center">
              <el-form-item
                id="contactNumber"
                label=""
                :prop="'situationList.' + index + '.contactNumber'"
                :rules="formRules.contactNumber"
              >
                <el-input
                  type="text"
                  v-model="item.contactNumber"
                  placeholder="联系电话"
                />
              </el-form-item>
            </td>
            <td class="m-label text-center">
              <el-form-item
                id="email"
                label=""
                :prop="'situationList.' + index + '.email'"
                :rules="formRules.email"
              >
                <el-input type="text" v-model="item.email" placeholder="邮箱" />
              </el-form-item>
            </td>

            <td>
              <div class="flex justify-center">
                <el-button
                  :disabled="Boolean(index === 0 || index === 1)"
                  type="danger"
                  icon="el-icon-delete"
                  @click="deleteMainManager(item)"
                >
                </el-button>
              </div>
            </td>
          </tr>
          <tr>
            <td colspan="10">
              <el-button type="primary" @click="addMainManager">
                添加管理人
              </el-button>
            </td>
          </tr>
        </table>

        <div class="form-sub-title">情况明细</div>

        <el-row :gutter="80" type="flex" class="justify-between flex-wrap pr-4">
          <el-col :span="8">
            <el-form-item
              label="职工总数:"
              class="fixed-input-w"
              prop="staffSum"
            >
              <el-input-number
                id="staffSum"
                v-model="staffForm.staffSum"
                auto-complete="off"
                :controls="false"
                class="m-input-number"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="缴纳社保人数:"
              class="fixed-input-w"
              prop="socialSecurityNum"
            >
              <el-input-number
                id="socialSecurityNum"
                v-model="staffForm.socialSecurityNum"
                auto-complete="off"
                :controls="false"
                class="m-input-number"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="留学回国人数:"
              class="fixed-input-w"
              prop="abroadNum"
            >
              <el-input-number
                id="abroadNum"
                v-model="staffForm.abroadNum"
                auto-complete="off"
                :controls="false"
                class="m-input-number"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <div class="form-sub-title">按工作性质分</div>

        <el-row :gutter="80" type="flex" class="justify-between flex-wrap pr-4">
          <el-col :span="8">
            <el-form-item
              label="行政管理人员:"
              class="fixed-input-w"
              prop="administrativeManagerNum"
            >
              <el-input-number
                id="administrativeManagerNum"
                v-model="staffForm.administrativeManagerNum"
                auto-complete="off"
                :controls="false"
                class="m-input-number"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="市场营销人员:"
              class="fixed-input-w"
              prop="marketingNum"
            >
              <el-input-number
                id="marketingNum"
                v-model="staffForm.marketingNum"
                auto-complete="off"
                :controls="false"
                class="m-input-number"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="研发设计人员:"
              class="fixed-input-w"
              prop="researchDesignNum"
            >
              <el-input-number
                id="researchDesignNum"
                v-model="staffForm.researchDesignNum"
                auto-complete="off"
                :controls="false"
                class="m-input-number"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="加工制造人员:"
              class="fixed-input-w"
              prop="makerNum"
            >
              <el-input-number
                id="makerNum"
                v-model="staffForm.makerNum"
                auto-complete="off"
                :controls="false"
                class="m-input-number"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="其他人员:"
              class="fixed-input-w"
              prop="workElseNum"
            >
              <el-input-number
                id="workElseNum"
                v-model="staffForm.workElseNum"
                auto-complete="off"
                :controls="false"
                class="m-input-number"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8"></el-col>
        </el-row>

        <div class="form-sub-title">按学历分</div>
        <el-row :gutter="80" type="flex" class="justify-between flex-wrap pr-4">
          <el-col :span="8">
            <el-form-item
              label="博士学历:"
              class="fixed-input-w"
              prop="doctorDegreeNum"
            >
              <el-input-number
                id="doctorDegreeNum"
                v-model="staffForm.doctorDegreeNum"
                auto-complete="off"
                :controls="false"
                class="m-input-number"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="硕士学历:"
              class="fixed-input-w"
              prop="masterDegreeNum"
            >
              <el-input-number
                id="masterDegreeNum"
                v-model="staffForm.masterDegreeNum"
                auto-complete="off"
                :controls="false"
                class="m-input-number"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="本科学历:"
              class="fixed-input-w"
              prop="undergraduateDegreeNum"
            >
              <el-input-number
                id="undergraduateDegreeNum"
                v-model="staffForm.undergraduateDegreeNum"
                auto-complete="off"
                :controls="false"
                class="m-input-number"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="专科学历:"
              class="fixed-input-w"
              prop="specialistDegreeNum"
            >
              <el-input-number
                id="specialistDegreeNum"
                v-model="staffForm.specialistDegreeNum"
                auto-complete="off"
                :controls="false"
                class="m-input-number"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="其他人员:"
              class="fixed-input-w"
              prop="elseDegreeNum"
            >
              <el-input-number
                id="elseDegreeNum"
                v-model="staffForm.elseDegreeNum"
                auto-complete="off"
                :controls="false"
                class="m-input-number"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8"></el-col>
        </el-row>

        <div class="form-sub-title">按技术职称分</div>
        <el-row :gutter="80" type="flex" class="justify-between flex-wrap pr-4">
          <el-col :span="8">
            <el-form-item
              label="高级职称:"
              class="fixed-input-w"
              prop="seniorProfessionalNum"
            >
              <el-input-number
                id="seniorProfessionalNum"
                v-model="staffForm.seniorProfessionalNum"
                auto-complete="off"
                :controls="false"
                class="m-input-number"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="中级职称:"
              class="fixed-input-w"
              prop="intermediateProfessionalNum"
            >
              <el-input-number
                id="intermediateProfessionalNum"
                v-model="staffForm.intermediateProfessionalNum"
                auto-complete="off"
                :controls="false"
                class="m-input-number"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="初级职称:"
              class="fixed-input-w"
              prop="elementaryProfessionalNum"
            >
              <el-input-number
                id="elementaryProfessionalNum"
                v-model="staffForm.elementaryProfessionalNum"
                auto-complete="off"
                :controls="false"
                class="m-input-number"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="其他人员:"
              class="fixed-input-w"
              prop="elseProfessionalNum"
            >
              <el-input-number
                id="elseProfessionalNum"
                v-model="staffForm.elseProfessionalNum"
                auto-complete="off"
                :controls="false"
                class="m-input-number"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <div class="flex justify-between py-10 text-center">
          <el-button
            v-if="isSubmit"
            class="confirm-btn tracking-widest"
            type="primary"
            size="medium"
            @click="submitForm"
          >
            保存 / 下一步
          </el-button>

          <DeclareNextItem ref="next" v-show="false" />
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import {
  checNumber,
  limitLength,
  requiredInput,
  limitLengthFocus,
} from "@/utils/formRules.js";
import { addStaff, getStaff, updateStaff } from "@/api/declareForm/staff";

export default {
  name: "EnterpriseStaffForm",
  props: {
    isSubmit: {
      type: Boolean,
      default: false,
    },
  },
  computed: {
    mainManagRowSpan() {
      return this.staffForm.situationList.length + 2;
    },
  },

  data() {
    // 检验身份证号 省份码
    var checkProv = function (val) {
      var pattern = /^[1-9][0-9]/;
      var provs = {
        11: "北京",
        12: "天津",
        13: "河北",
        14: "山西",
        15: "内蒙古",
        21: "辽宁",
        22: "吉林",
        23: "黑龙江 ",
        31: "上海",
        32: "江苏",
        33: "浙江",
        34: "安徽",
        35: "福建",
        36: "江西",
        37: "山东",
        41: "河南",
        42: "湖北 ",
        43: "湖南",
        44: "广东",
        45: "广西",
        46: "海南",
        50: "重庆",
        51: "四川",
        52: "贵州",
        53: "云南",
        54: "西藏 ",
        61: "陕西",
        62: "甘肃",
        63: "青海",
        64: "宁夏",
        65: "新疆",
        71: "中国台湾",
        81: "中国香港",
        82: "中国澳门",
      };
      if (pattern.test(val)) {
        if (provs[val]) {
          return true;
        }
      }
      return false;
    };
    // 检验身份证号 日期码
    var checkDate = function (val) {
      var pattern =
        /^(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)$/;
      if (pattern.test(val)) {
        var year = val.substring(0, 4);
        var month = val.substring(4, 6);
        var date = val.substring(6, 8);
        var date2 = new Date(year + "-" + month + "-" + date);
        if (date2 && date2.getMonth() == parseInt(month) - 1) {
          return true;
        }
      }
      return false;
    };
    // 检验身份证号 校检码
    var checkCode = function (val) {
      var p =
        /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
      var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
      var parity = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2];
      var code = val.substring(17);
      if (p.test(val)) {
        var sum = 0;
        for (var i = 0; i < 17; i++) {
          sum += val[i] * factor[i];
        }
        if (parity[sum % 11] == code.toUpperCase()) {
          return true;
        }
      }
      return false;
    };
    // 检验身份证号
    var checkID = (rule, value, callback) => {
      if (!value) {
        // return callback(new Error("身份证号不能为空"));
        return callback();
      }
      if (checkCode(value)) {
        var date = value.substring(6, 14);
        if (checkDate(date)) {
          if (checkProv(value.substring(0, 2))) {
            return callback();
          }
        }
      }
      document
        .getElementById("mainIdentityCardNum")
        .querySelector("input")
        .focus();
      return callback(new Error("请输入有效身份证号"));
    };

    // 表单校验数字使用
    var checkFormNumberV = (rule, value, callback) => {
      return checNumber(rule, value, callback);
    };
    var checkStaffSum = (rule, value, callback) => {
      if (!value) {
        // return callback(new Error("职工总数不能为空"));
        return callback();
      }
      if (!Number.isInteger(value)) {
        return callback(new Error("请输入数字值"));
      } else {
        if (this.staffForm.staffSum !== "") {
          let tempObj = JSON.parse(JSON.stringify(this.formRules));
          tempObj["staffSum"] = undefined;
          delete tempObj.staffSum;
          for (const key in tempObj) {
            if (Object.hasOwnProperty.call(tempObj, key)) {
              this.$refs.staffForm.validateField(key);
            }
          }
          callback();
        }
        return callback();
      }
    };

    var checkNormalNum = (rule, value, callback) => {
      // if (!value) {
      //   return callback(new Error('年龄不能为空'));
      // }

      if (!Number.isInteger(value)) {
        callback(new Error("请输入有效数值"));
      } else {
        if (value < 0) {
          callback(new Error("请输入有效数值"));
        }
        if (value) {
          if (value > this.staffForm.staffSum) {
            callback(new Error("输入数值不能 大于 职工总数"));
          } else {
            callback();
          }
        } else {
          callback();
        }
      }
    };
    // 校验手机号
    const checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback();
      }
      let reg = /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/;
      setTimeout(() => {
        if (!reg.test(value)) {
          callback(new Error("请输入有效手机号"));
        } else {
          callback();
        }
      }, 1000);
    };

    // 校验座机号
    const checkLandline = (rule, value, callback) => {
      if (!value) {
        return callback();
      }
      let reg = /^((0\d{2,3}-\d{7,8})|(1[3456789]\d{9}))$/;
      setTimeout(() => {
        if (!reg.test(value)) {
          callback(new Error("请输入有效座机号"));
        } else {
          callback();
        }
      }, 1000);
    };

    // 校验手机号 和 座机号
    const checkAllPhone = (rule, value, callback) => {
      /** 手机号码校验  */
      if (!value) {
        return callback();
      }
      if (value.indexOf("-") === -1) {
        return checkPhone(rule, value, callback);
      }
      return checkLandline(rule, value, callback);
    };

    const checkMail = (rule, value, callback) => {
      if (!value) {
        return callback();
      }
      let reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
      if (!reg.test(value)) {
        callback(new Error("请输入有效邮箱"));
      } else {
        callback();
      }
    };

    return {
      isAlreadySubmit: 0,
      formLoading: false,
      // 职称类别
      jobNameOptions: [
        {
          dictValue: 0,
          dictLabel: "高级职称",
        },
        {
          dictValue: 1,
          dictLabel: "中级职称",
        },
        {
          dictValue: 2,
          dictLabel: "初级职称",
        },
        {
          dictValue: 3,
          dictLabel: "其他职称",
        },
      ],

      // 人员类别
      mainTypeOptions: [
        {
          value: 0,
          label: "法人",
        },
        {
          value: 1,
          label: "主要管理人",
        },
      ],

      // add 人员类别
      mainTypeOp: [
        {
          value: 0,
          label: "法人",
        },
        {
          value: 1,
          label: "主要管理人",
        },
        {
          value: 3,
          label: "项目负责人",
        },
        {
          value: 4,
          label: "项目联系人",
        },
      ],

      // 性别
      sexTypes: [
        {
          value: 0,
          label: "女",
        },
        {
          value: 1,
          label: "男",
        },
      ],

      // 学历类别
      educationOptions: [
        // {
        //   dictValue: 0,
        //   dictLabel: "博士研究生"
        // },
        // {
        //   dictValue: 1,
        //   dictLabel: "硕士研究生"
        // },
        // {
        //   dictValue: 2,
        //   dictLabel: "本科生"
        // },
        // {
        //   dictValue: 3,
        //   dictLabel: "大专"
        // },
        // {
        //   dictValue: 4,
        //   dictLabel: "其他"
        // }
      ],
      modalObj: {
        staffId: 0,
        type: 0,
        name: "",
        sex: "",
        degree: "",
        identityCardNum: "",
        job: "",
        professional: "",
        contactNumber: "",
        email: "",
        isDisabled: 0,
      },
      addMainManagerIndex: 0,
      staffForm: {
        situationList: [],

        staffSum: 0,
        socialSecurityNum: 0,
        abroadNum: 0,
        // 工作性质分
        administrativeManagerNum: 0,
        marketingNum: 0,
        researchDesignNum: 0,
        makerNum: 0,
        workElseNum: 0,
        // 按学历分
        doctorDegreeNum: 0,
        masterDegreeNum: 0,
        undergraduateDegreeNum: 0,
        specialistDegreeNum: 0,
        elseDegreeNum: 0,
        //  按技术职称分
        seniorProfessionalNum: 0,
        intermediateProfessionalNum: 0,
        elementaryProfessionalNum: 0,
        elseProfessionalNum: 0,
        relationId: 0,
      },
      formRules: {
        // 主要人员情况 表单校验
        mainName: [
          {
            required: true,
            validator: requiredInput("请输入姓名", "mainName"),
            trigger: "blur",
          },
          {
            validator: limitLengthFocus(
              10,
              "姓名不能超过10个字符, 请删减后重试",
              "mainName"
            ),
            trigger: "blur",
          },
        ],
        mainIdentityCardNum: [
          // { required: true, message: "请输入身份证号", trigger: "blur" },
          {
            validator: limitLengthFocus(
              30,
              "身份证号不能超过30个字符, 请删减后重试",
              "mainIdentityCardNum"
            ),
            trigger: "blur",
          },
          { validator: checkID, trigger: "blur" },
        ],
        mainJob: [
          {
            validator: limitLengthFocus(
              10,
              "职务不能超过10个字符, 请删减后重试",
              "mainJob"
            ),
            trigger: "blur",
          },
        ],
        email: [
          {
            validator: requiredInput("请输入邮箱", "email"),
            trigger: "blur",
          },
          {
            validator: limitLengthFocus(
              30,
              "邮箱不能超过30个字符, 请删减后重试",
              "email"
            ),
            trigger: "blur",
          },
          {
            validator: checkMail,
            trigger: "blur",
          },
        ],
        contactNumber: [
          {
            validator: limitLengthFocus(
              16,
              "联系电话不能超过16个字符, 请删减后重试",
              "contactNumber"
            ),
            trigger: "blur",
          },
          {
            validator: checkAllPhone,
            trigger: "blur",
          },
        ],

        staffSum: [
          // { required: true, message: "请输入职工总数", trigger: "blur" },
          // { validator: checkFormNumberV, trigger: "blur" },
          {
            validator: limitLengthFocus(
              8,
              "职工总数不能超过8个字符, 请删减后重试",
              "staffSum"
            ),
            trigger: "blur",
          },
          { validator: checkStaffSum, trigger: "blur" },
        ],
        socialSecurityNum: [
          // { required: true, message: "请输入社保缴纳人数", trigger: "blur" },

          { validator: checkNormalNum, trigger: "blur" },
          {
            validator: limitLengthFocus(
              8,
              "社保缴纳人数不能超过8个字符, 请删减后重试",
              "socialSecurityNum"
            ),
            trigger: "blur",
          },
        ],
        abroadNum: [
          // { required: true, message: "请输入留学回国人数", trigger: "blur" },
          { validator: checkNormalNum, trigger: "blur" },
          {
            validator: limitLengthFocus(
              8,
              "留学回国人数不能超过8个字符, 请删减后重试",
              "abroadNum"
            ),
            trigger: "blur",
          },
        ],

        administrativeManagerNum: [
          // { required: true, message: "请输入行政管理人员", trigger: "blur" },
          { validator: checkNormalNum, trigger: "blur" },
          { validator: limitLength(8), trigger: "blur" },
        ],
        marketingNum: [
          // { required: true, message: "请输入市场营销人员", trigger: "blur" },
          { validator: checkNormalNum, trigger: "blur" },
          { validator: limitLength(8), trigger: "blur" },
        ],
        researchDesignNum: [
          // { required: true, message: "请输入研发设计人员", trigger: "blur" },
          { validator: checkNormalNum, trigger: "blur" },
          { validator: limitLength(8), trigger: "blur" },
        ],
        makerNum: [
          // { required: true, message: "请输入加工制造人员", trigger: "blur" },
          { validator: checkNormalNum, trigger: "blur" },
          { validator: limitLength(8), trigger: "blur" },
        ],
        workElseNum: [
          // { required: true, message: "请输入其他人员", trigger: "blur" }
          { validator: checkNormalNum, trigger: "blur" },
          { validator: limitLength(8), trigger: "blur" },
        ],

        doctorDegreeNum: [
          // { required: true, message: "请输入博士学历人数", trigger: "blur" }
          { validator: checkNormalNum, trigger: "blur" },
          { validator: limitLength(8), trigger: "blur" },
        ],
        masterDegreeNum: [
          // { required: true, message: "请输入硕士学历人数", trigger: "blur" }
          { validator: checkNormalNum, trigger: "blur" },
          { validator: limitLength(8), trigger: "blur" },
        ],
        undergraduateDegreeNum: [
          // { required: true, message: "请输入本科学历人数", trigger: "blur" }
          { validator: checkNormalNum, trigger: "blur" },
          { validator: limitLength(8), trigger: "blur" },
        ],
        specialistDegreeNum: [
          // { required: true, message: "请输入专科学历人数", trigger: "blur" }
          { validator: checkNormalNum, trigger: "blur" },
          { validator: limitLength(8), trigger: "blur" },
        ],
        elseDegreeNum: [
          // { required: true, message: "请输入其他人员", trigger: "blur" }
          { validator: checkNormalNum, trigger: "blur" },
          { validator: limitLength(8), trigger: "blur" },
        ],

        seniorProfessionalNum: [
          // { required: true, message: "请输入高级人数", trigger: "blur" }
          { validator: checkNormalNum, trigger: "blur" },
          { validator: limitLength(8), trigger: "blur" },
        ],
        intermediateProfessionalNum: [
          // { required: true, message: "请输入中级人数", trigger: "blur" }
          { validator: checkNormalNum, trigger: "blur" },
          { validator: limitLength(8), trigger: "blur" },
        ],
        elementaryProfessionalNum: [
          // { required: true, message: "请输入初级人数", trigger: "blur" }
          { validator: checkNormalNum, trigger: "blur" },
          { validator: limitLength(8), trigger: "blur" },
        ],
        elseProfessionalNum: [
          // { required: true, message: "请输入其他人员", trigger: "blur" }
          { validator: checkNormalNum, trigger: "blur" },
          { validator: limitLength(8), trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.initRelationId();
    this.getStaffInfo();
    this.initDictFileType();
  },
  methods: {
    initDictFileType() {
      this.getDicts("declare_education_type").then((res) => {
        if (res.code == 200) {
          this.educationOptions = res.data;
        }
      });
      this.getDicts("declare_jobname_type").then((res) => {
        if (res.code == 200) {
          this.jobNameOptions = res.data;
        }
      });
    },
    // 初始化 申报文件 ID
    initRelationId() {
      let realationId = this.$route.query.id;
      if (realationId) {
        this.staffForm.relationId = parseInt(realationId);
      }
    },
    // 获取 人员信息
    getStaffInfo() {
      getStaff(this.staffForm.relationId).then((res) => {
        if (res.code == 200) {
          if (
            !res.data ||
            !res.data.situationList ||
            res.data.situationList.length === 0
          ) {
            this.staffForm.situationList = [];
            this.addMustFormItem();
          }
          if (res.data) {
            this.isAlreadySubmit = 1;
            this.staffForm = res.data;

            this.staffForm.situationList.forEach((item) => {
              if (item) {
                item.degree = String(item.degree);
                item.professional = String(item.professional);
              }
            });
          }
        }
      });
    },
    // 判断  初始化 ”主要人员情况“ 是否有数据，没有的话 新增两个  '项目负责人' '项目联系人'
    addMustFormItem() {
      this.addMainManager();
      this.staffForm.situationList[0].type = 3;
      this.staffForm.situationList[0].isDisabled = 1;

      this.addMainManager();
      this.staffForm.situationList[1].type = 4;
      this.staffForm.situationList[1].isDisabled = 1;
    },

    addMainManager() {
      this.addMainManagerIndex += 1;
      let timeChuo = new Date();
      let addObj = JSON.parse(JSON.stringify(this.modalObj));
      addObj["staffId"] = timeChuo.valueOf() + this.addMainManagerIndex;
      this.staffForm.situationList.push(addObj);
    },
    deleteMainManager(item) {
      let id = item.situationId || item.staffId;
      let tempArr = JSON.parse(JSON.stringify(this.staffForm.situationList));

      let newArr = tempArr.filter((item) => {
        if (item.hasOwnProperty("situationId")) {
          return item.situationId !== id;
        }
        if (item.hasOwnProperty("staffId")) {
          return item.staffId !== id;
        }
      });
      this.staffForm.situationList = newArr;
    },

    // 按技术职称分
    techNumInput() {},

    submitForm() {
      let formName = "staffForm";
      for (const key in this.staffForm) {
        if (Object.hasOwnProperty.call(this.staffForm, key)) {
          const element = this.staffForm[key];
          let isArray = element instanceof Array;
          if (!isArray && (!element || element <= 0)) {
            this.staffForm[key] = 0;
          }
        }
      }

      // this.formLoading = true;
      // if (!this.isAlreadySubmit) {
      //   addStaff(this.staffForm).then(res => {
      //     if (res.code == 200) {
      //       this.$message.success("保存成功");
      //     }
      //     this.formLoading = false;
      //   });
      // } else {
      //   updateStaff(this.staffForm).then(res => {
      //     if (res.code == 200) {
      //       this.$message.success("保存成功");
      //     }
      //     this.formLoading = false;
      //   });
      // }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.formLoading = true;
          if (!this.isAlreadySubmit) {
            addStaff(this.staffForm).then((res) => {
              if (res.code == 200) {
                this.$message.success("保存成功");
                // 跳转至下一页
                this.$refs.next.nextDeclareItem();
              } else {
                this.$message.error(res.msg);
              }
              this.formLoading = false;
            });
          } else {
            updateStaff(this.staffForm).then((res) => {
              if (res.code == 200) {
                this.$message.success("保存成功");
                // 跳转至下一页
                this.$refs.next.nextDeclareItem();
              } else {
                this.$message.error(res.msg);
              }
              this.formLoading = false;
            });
          }
        } else {
          // console.log(" valid valid");
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
/** 通用inputNumber样式 */
.m-input-number {
  width: 100%;

  ::v-deep .el-input__inner {
    text-align: left;
  }
}
</style>
